package org.colafries.admin.controller;

import cn.hutool.core.codec.Base64Decoder;
import org.colafries.admin.entity.vo.*;
import org.colafries.admin.service.IDataBaseService;
import org.colafries.admin.wrapper.Response;
import org.colafries.admin.wrapper.ResponseWrapper;
import org.colafries.common.metadata.Column;
import org.colafries.common.metadata.Schema;
import org.colafries.metadata.result.JdbcSelectResult;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author colafries
 * @version v1.0
 * @ClassName DataSourceController
 * @Description
 * @date Created in 2024/3/14
 **/
@RestController
@RequestMapping("/database")
public class DataBaseController {

    @Resource
    IDataBaseService iDataBaseService;

    /**
     * 查询数据源
     *
     * @param dataBaseReqVO
     * @author colafries
     * @Date 2024/4/10
     */
    @PostMapping("/getAll")
    public Response<?> getAll(@RequestBody DataBaseReqVO dataBaseReqVO) {
        List<DataBaseRspVO> dataBaseRspVOList = iDataBaseService.getAll(dataBaseReqVO);
        return ResponseWrapper.ok(dataBaseRspVOList);
    }

    /**
     * 新增数据源
     *
     * @param dataBaseReqVO
     * @author colafries
     * @Date 2024/4/10
     */
    @PostMapping("/add")
    public Response<?> add(@RequestBody DataBaseReqVO dataBaseReqVO) {
        int resultData = iDataBaseService.add(dataBaseReqVO);
        return ResponseWrapper.ok(resultData);
    }

    /**
     * 修改数据源
     *
     * @param dataBaseReqVO
     * @author colafries
     * @Date 2024/4/10
     */
    @PostMapping("/update")
    public Response<?> update(@RequestBody DataBaseReqVO dataBaseReqVO) {
        int resultData = iDataBaseService.update(dataBaseReqVO);
        return ResponseWrapper.ok(resultData);
    }

    /**
     * 删除数据源
     *
     * @param dataBaseReqVO
     * @author colafries
     * @Date 2024/4/10
     */
    @PostMapping("/delete")
    public Response<?> delete(@RequestBody DataBaseReqVO dataBaseReqVO) {
        int resultData = iDataBaseService.delete(dataBaseReqVO);
        return ResponseWrapper.ok(resultData);
    }

    /**
     * 数据源连通性测试
     *
     * @param dataBaseReqVO
     * @author colafries
     * @Date 2024/4/10
     */
    @PostMapping("/testConnection")
    public Response<?> testConnection(@RequestBody DataBaseReqVO dataBaseReqVO) {
        Map<String, String> resultData = iDataBaseService.testConnection(dataBaseReqVO);
        return ResponseWrapper.ok(resultData);
    }

    /**
     * 查询元数据库表
     *
     * @param dataBaseReqVO
     * @author colafries
     * @Date 2024/4/10
     */
    @PostMapping("/getSchemasAndTables")
    public Response<?> getSchemasAndTables(@RequestBody DataBaseReqVO dataBaseReqVO) {
        List<Schema> schemas = iDataBaseService.getSchemasAndTables(dataBaseReqVO);
        return ResponseWrapper.ok(schemas);
    }

    /**
     * 查询元数据表及表的字段信息
     *
     * @param dataBaseReqVO
     * @author colafries
     * @Date 2024/4/10
     */
    @PostMapping("/getTableSchema")
    public Response<?> getTableSchema(@RequestBody DataBaseReqVO dataBaseReqVO) {
        List<Column> columns = iDataBaseService.getTableSchema(dataBaseReqVO);
        return ResponseWrapper.ok(columns);
    }

    /**
     * 查询sql结果数据(分页)
     *
     * @param queryDataReqVO 模型请求vo
     * @author colafries
     * @Date 2024/3/24
     */
    @PostMapping("/querySqlPage")
    public Response<?> querySqlPage(@RequestBody QueryDataReqVO queryDataReqVO) {
        String querySqlDecode = Base64Decoder.decodeStr(queryDataReqVO.getQuerySql(), StandardCharsets.UTF_8);
        queryDataReqVO.setQuerySql(querySqlDecode);
        JdbcSelectResult jdbcSelectResult = iDataBaseService.querySqlPage(queryDataReqVO.getDatabaseId(),
                queryDataReqVO.getQuerySql(), queryDataReqVO.getCurrent(), queryDataReqVO.getSize());
        QueryResultRspVO queryResultRspVO = new QueryResultRspVO();
        BeanUtils.copyProperties(jdbcSelectResult, queryResultRspVO);
        //特殊处理
        queryResultRspVO.setColumns(
                jdbcSelectResult.getColumns().stream().map(Column::getName).collect(Collectors.toList())
        );
        return ResponseWrapper.ok(queryResultRspVO);
    }

    /**
     * 查询sql结果schema
     *
     * @param queryDataReqVO
     * @return
     */
    @PostMapping("/querySqlSchema")
    public Response<?> querySqlSchema(@RequestBody QueryDataReqVO queryDataReqVO) {
        String querySqlDecode = Base64Decoder.decodeStr(queryDataReqVO.getQuerySql(), StandardCharsets.UTF_8);
        queryDataReqVO.setQuerySql(querySqlDecode);
        List<ColumnRspVO> columns = iDataBaseService.querySqlSchema(queryDataReqVO.getDatabaseId(),queryDataReqVO.getQuerySql());
        return ResponseWrapper.ok(columns);
    }

}
